package com.example.smartpillow;

	import java.util.HashMap;
	import java.util.LinkedHashMap;
	import java.util.Map;

	import android.content.Context;
	import android.database.Cursor;
	import android.database.sqlite.SQLiteDatabase;
	import android.database.sqlite.SQLiteOpenHelper;

	public class MySqlDatabase extends SQLiteOpenHelper{
		private final static String tableName="SQLtest";
		private final static String[] colmnlist=new String[]{"temp","time"};
		public MySqlDatabase (Context context){
			super(context, "SQLContext", null, 1);
			
		}
		@Override
		public void onCreate(SQLiteDatabase sqL) {
			sqL.execSQL("DROP TABLE IF EXISTS "+ tableName+";");
			sqL.execSQL("CREATE TABLE " + tableName + " ("+colmnlist[0]+" DOUBLE,"+colmnlist[1]+" INTEGER);");
			
					
		}
		@Override
		public void onUpgrade(SQLiteDatabase sqL, int arg1, int arg2) {
			sqL.execSQL("DROP TABLE IF EXISTS "+ tableName+";");
			sqL.execSQL("CREATE TABLE " + tableName + " ("+colmnlist[0]+" DOUBLE,"+colmnlist[1]+" INTEGER);");
			// TODO Auto-generated method stub
			
		}
		public void sqlInsert(double temp){
			getWritableDatabase().execSQL("INSERT INTO "+ tableName+ " (" + colmnlist[0] + ", " + colmnlist[1]+") VALUES ("+temp+ ", "+ (System.currentTimeMillis()) +");");
			System.out.println(System.currentTimeMillis());
		}
	    public Map<Long, Double> sqlQuery(){
	    	Map<Long, Double> sqlmap=new LinkedHashMap<Long, Double>();
	    	
	    	//Cursor cursor= getReadableDatabase().query(tableName,columnlist, columnlist[0] + " > " + (System.currentTimeMillis()/1000 - (24*3600)),null,null,null,null);
	    	Cursor cursor = getReadableDatabase().query(true, tableName, null, null, null, null, null, colmnlist[1] + " DESC", "10");
	    	while (cursor.moveToNext()){
	    		sqlmap.put(cursor.getLong(1), cursor.getDouble(0));
	    		System.out.println(cursor.getDouble(0) + " => " + cursor.getLong(1));
	    	}
	    	return sqlmap;
	    }
		
	}



